package com.qujia.markert.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qujia.common.constant.UserConstants;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.exception.CustomException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.common.utils.poi.ExcelUtil;
import com.qujia.markert.domain.UserRecommendRecord;
import com.qujia.markert.domain.dto.UserRecommendRecordExportDTO;
import com.qujia.markert.domain.vo.UserRecommendRecordQueryReq;
import com.qujia.markert.mapper.UserRecommendRecordMapper;
import com.qujia.markert.service.IUserRecommendRecordService;
import com.qujia.station.domain.vo.EnterpriseAndStationVo;
import com.qujia.station.service.IStationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @date 2024/1/17
 */
@Service
public class UserRecommendRecordServiceImpl extends ServiceImpl<UserRecommendRecordMapper, UserRecommendRecord> implements IUserRecommendRecordService {

    @Autowired
    private IStationService stationService;
    @Override
    public List<UserRecommendRecord> queryList(UserRecommendRecordQueryReq req) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        if (sysUser.getEnterpriseId() != null) {
            req.setEnterpriseId(sysUser.getEnterpriseId());
        }
        if (SecurityUtils.isPartner()) {
            EnterpriseAndStationVo itemIds = stationService.getItemIds(sysUser);
            if (itemIds != null){
                if(CollectionUtils.isEmpty(itemIds.getEnterpriseIds())){
                    return new ArrayList<>();
                }
                req.setEnterpriseIdListForAuthor(itemIds.getEnterpriseIds());
            } else {
                throw new CustomException(UserConstants.NO_ENTERPRISE);
            }
        }
        UserRecommendRecord form = new UserRecommendRecord();
        BeanUtils.copyProperties(req,form);
        LambdaQueryWrapper<UserRecommendRecord> qw = Wrappers.lambdaQuery(form);
        if (req.getBeginTime() != null && req.getEndTime() != null) {
            qw.gt(UserRecommendRecord::getCreateTime,req.getBeginTime())
                    .lt(UserRecommendRecord::getCreateTime,req.getEndTime());
        }
        qw.in(!CollectionUtils.isEmpty(req.getEnterpriseIdListForAuthor()),UserRecommendRecord::getEnterpriseId,req.getEnterpriseIdListForAuthor());
        qw.orderByDesc(UserRecommendRecord::getId);
        return list(qw);
    }

    @Override
    public AjaxResult export(UserRecommendRecordQueryReq req) {
        List<UserRecommendRecord> recordList = queryList(req);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        List<UserRecommendRecordExportDTO> exportDTOS = recordList.stream().map(r -> {
            UserRecommendRecordExportDTO exportDTO = new UserRecommendRecordExportDTO();
            BeanUtils.copyProperties(r,exportDTO);
            if (r.getCreateTime() != null) {
                exportDTO.setCreateTime(formatter.format(r.getCreateTime()));
            }
            if (r.getNewUserStatus() != null) {
                exportDTO.setNewUserStatus(r.getNewUserStatus().getDesc());
            }
            if (r.getDeliveryStatus() != null) {
                exportDTO.setDeliveryStatus(r.getDeliveryStatus().getDesc());
            }
            return exportDTO;
        }).collect(Collectors.toList());
        ExcelUtil<UserRecommendRecordExportDTO> excelUtil = new ExcelUtil<>(UserRecommendRecordExportDTO.class);
        return excelUtil.exportExcel(exportDTOS, "sheet");
    }
}
